/*
ssc install bigtab
ssc install astile	
ssc install reghdfe
ssc install ftools
reghdfe, compile
*/

*****************2-digit occ codes, firm & occ FE***********************

use anon t ev w1 kor ferfi  wh1 vallazon* feor1_h2 if kor>=18 & kor<=70  & vallazon1~=.  using "$in/admin3_alap.dta" , clear

rename vallazon1 vallazon

keep if wh1==40

keep if w1>=0 & w1~=.


collapse (mean)  w1 (last)  kor feor1_h2 , by(vallazon ev anon)

gen age2=(kor-40)^2
gen age3=(kor-40)^3
 
gen lnw=ln(w1)

	set more off, perm 
	cap drop conseed
	gen conseed=0
	replace conseed=1 if anon==192047

	forval i=1/10 {
	bysort anon (vallazon ev): egen conn=max(conseed)
	replace conseed=conn
	drop conn
	bysort vallazon (anon ev): egen conn=max(conseed)
	replace conseed=conn
	drop conn
	disp(`i')
	}

	tab conseed 

	
global controls "c.age2 c.age3 i.ev "
drop if conseed==0

reghdfe lnw $controls if conseed==1, absorb (fei=anon fej=vallazon feo=feor1_h2, savefe)

save "$out_AKM/FE_temp.dta", replace

use "$out_AKM/FE_temp.dta", clear
rename fei anon_fix_o2firm
keep anon anon_fix_o2firm
duplicates drop
drop if anon_fix_o2firm==.
compress
save "$out_AKM/FEi_o2firm.dta", replace

use "$out_AKM/FE_temp.dta", clear
rename fej vall_fix_o2firm
keep vallazon vall_fix_o2firm
duplicates drop
drop if vall_fix_o2firm==.
compress
save "$out_AKM/FEj_o2firm.dta", replace


use "$out_AKM/FE_temp.dta", clear
rename feo occ_fix_o2firm
keep feor1_h2 occ_fix_o2firm
duplicates drop
drop if occ_fix_o2firm==.
compress
save "$out_AKM/FEo_o2firm.dta", replace



*****************2-digit occ codes, occ FE***********************

use anon t ev w1 kor ferfi  wh1 vallazon* feor1_h2 if kor>=18 & kor<=70  & vallazon1~=.  using "$in/admin3_alap.dta" , clear

rename vallazon1 vallazon

keep if wh1==40

keep if w1>=0 & w1~=.


collapse (mean)  w1 (last)  kor feor1_h2 , by(ev anon)

gen age2=(kor-40)^2
gen age3=(kor-40)^3
 
gen lnw=ln(w1)

global controls "c.age2 c.age3 i.ev "

reghdfe lnw $controls, absorb (fei=anon feo=feor1_h2, savefe)

save "$out_AKM/FE_temp.dta", replace

use "$out_AKM/FE_temp.dta", clear
rename fei anon_fix_o2
keep anon anon_fix_o2
duplicates drop
drop if anon_fix_o2==.
compress
save "$out_AKM/FEi_o2.dta", replace

use "$out_AKM/FE_temp.dta", clear
rename feo occ_fix_o2
keep feor1_h2 occ_fix_o2
duplicates drop
drop if occ_fix_o2==.
compress
save "$out_AKM/FEo_o2.dta", replace

